﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using log4net.Appender;
using log4net.Config;
using log4net.Core;
using log4net.Layout;
using Microsoft.Practices.Unity;
using NUnit.Framework;

namespace ConfigGen.Core.Tests.EndToEndTests
{
    public static class TestHelper
    {
        public static IEnumerable<SingleFileGenerationResults> ExecuteCfgTest(Preferences settings)
        {
            ConfigureLogging();
            var container = new ConfigGenContainer();
            var generator = container.Resolve<ConfigurationFilesGenerator>();
            generator.GenerateFiles(settings);
            return generator.Results;
        }

        public static void CheckExitResults(IEnumerable<SingleFileGenerationResults> results, bool warningsExpected)
        {
            bool hasWarnings = false;
            foreach (var resultSet in results)
            {
                hasWarnings |= resultSet.HasWarnings;

                var unrecognisedTokens = from unusedTokenCount in resultSet.UnrecognisedTokenCounts
                                         where unusedTokenCount.Value > 0
                                         select unusedTokenCount.Key;

                var unusedTokens = from tokenCount in resultSet.TokenUseCounts
                                   where tokenCount.Value == 0
                                   select tokenCount.Key;

                foreach (var token in unrecognisedTokens) Console.WriteLine("{0}: UNRECOGNISED TOKEN: {1}", resultSet.MachineName, token );
                foreach (var token in unusedTokens) Console.WriteLine("{0}: UNUSED TOKEN: {1}", resultSet.MachineName, token);
            }

            Assert.AreEqual(hasWarnings, warningsExpected, "Incorrect exit results from execution");
        }



        private static void ConfigureLogging()
        {
            var appender = new ConsoleAppender();
            const string layout = @"%message%newline";
            appender.Threshold = Level.Debug;
            appender.Layout = new PatternLayout(layout);
            appender.ActivateOptions();
            BasicConfigurator.Configure(appender);
        }
    }
}